﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{
    /// <summary>
    /// 循环队列
    /// </summary>
    internal class CircleQueue2
    {
        int[] queue;//存放队列元素的容器
        int size;//队列容量
        int front;//队头
        int rear;//队尾
        int count;//队列中元素数量
        public CircleQueue2()
        {
            size = 10;
            queue = new int[size];
            front = -1;
            rear = -1;
            count = 0;
        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="data"></param>
        /// <exception cref="Exception"></exception>
        public void EnQueue(int data)
        {
            if (IsFull())
            {
                throw new Exception("队列满了！");
            }
            rear = (rear + 1) % size;
            queue[rear] = data;
            count++;
        }

        /// <summary>
        /// 出队
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int Dequeue()
        {
            if (IsEmpty())
            {
                throw new Exception("队列为空！");
            }
            count--;
            front = (front + 1) % size;
            return queue[front];
        }

        /// <summary>
        /// 队列是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return count == 0;
        }

        /// <summary>
        /// 队列是否满了
        /// </summary>
        /// <returns></returns>
        public bool IsFull()
        {
            return count == size;
        }

        public void Print()
        {
            if (IsEmpty())
            {
                return;
            }
            int i = front + 1;
            while ((i % size) != rear)
            {
                Console.Write(queue[i] + " ");
                i++;
            }
            Console.Write(queue[rear] + " ");
            Console.WriteLine($"队列头：{queue[front + 1]}，队尾：{queue[rear]},长度{count} ");
        }
    }
}
